<?php

namespace Managers;

class Vk extends \Lib\Model\DoctrineManager{

    
    public function saveComment($params) {

        
        
        $em = $this->getEntityManager();
        
        $qb = $em->createQueryBuilder();
        
        $em->getConnection()->beginTransaction();
        
        try {
            $comm = new \Entities\VkComments();
            
            if ($params['name'] != ''){
              $comm->setName($params['name']);
            }else{
              $comm->setName('Гость');
            } 

            if ($params['alltext'] != ''){
              $comm->setText($params['alltext']);
            }else{
              $comm->setText('пусто');
            } 
            
            $em->persist($comm);
            $em->flush();
            
            $em->getConnection()->commit();
        } catch (Exception $exc) {
            $em->getConnection()->rollBack();
        }
        
    }
    
    public function getAllMessages() {
        $em = $this->getEntityManager();
        $qb = $em->createQueryBuilder()
                ->select('c')
                ->from('Entities\VkComments', 'c')
                ->orderBy('c.commId', 'DESC');
        $result = $qb->getQuery()->getResult();
        return $result;
    }
}